/*
 * @Author: cqg
 * @Date: 2020-11-10 23:18:05
 * @LastEditors: cqg
 * @LastEditTime: 2020-12-31 09:28:13
 * @Description: file content
 */
import {
	toBeInitiatedChangeList,
	exportWarehousing
} from "@/views/wy-asset-management/initiate-change/api/initiate-change.js";
import { getDict } from "~/utils";

export default {
	name: "resource-manage",
	provide() {
		return {
			initList: this.query
		};
	},
	data() {
		return {
			loading: true,
			//表单数据
			tableData: [],
			menuList: [{ export: "导出" }],
			//状态栏多选
			multipleSelection: [],
			// 勾选
			selectDataArr: [],
			//过滤条件
			filterList: [
				{
					label: "资产分类",
					inputType: "dict",
					dictName: "asset-classification",
					name: "assetClassificationList"
				},
				{
					label: "使用状况",
					inputType: "dict",
					dictName: "asset-use-status",
					name: "useStatusList"
				}
			],

			// 分页
			pager: {
				total: 0, // 总页数
				current: 1, // 当前页数
				size: 20 // 每页显示多少条
			},
			// 请求参数
			params: {
				current: 1,
				size: 20,
				keyword: ""
			}
		};
	},
	//载入事件
	mounted() {
		//查询
		this.query();
	},

	methods: {
		// 菜单点击监听
		handleMenuClick(type) {
			type == "export" && this.exportReg();
		},
		/**
		 * 跳转到发起变更
		 * @param scope
		 */
		toInitiateChange(scope) {
			//变更状态 审批状态 10待发起(初始状态) 11变更未通过  12待变更  13核实未通过  14待核实  15办结
			//处置状态  10 未处置 11 待审核 12审核不通过 13办结
			if (
				!(
					(scope.row.changeStatus == "15" ||
						scope.row.changeStatus == "10" ||
						scope.row.changeStatus == "11") &&
					(scope.row.disposalStatus == "10" ||
						scope.row.disposalStatus == "12" ||
						scope.row.disposalStatus == "13")
				)
			) {
				this.$alert("只有未操作状态才能操作", "提示", {
					confirmButtonText: "确定"
				});
				return;
			}

			this.$router.push({
				path: "/asset-management/initiate-change-edit",
				query: {
					assetFixedWarehousingId: scope.row.assetFixedWarehousingId,
					warehousingAttachment: scope.row.warehousingAttachment,
					tyep: "edit"
				}
			});
		},

		/**
		 * 查询方法
		 * @param param
		 */
		query(params = {}) {
			let req = { ...this.params, ...params };
			this.loading = true;
			!req.assetClassificationList && delete req.assetClassificationList;
			!req.useStatusList && delete req.useStatusList;
			toBeInitiatedChangeList(req)
				.then(res => {
					const { current, size, total, records } = res.data.data;
					this.tableData = records;
					this.params = req;
					this.pager = { total, current, size };
					this.loading = false;
				})
				.catch(error => {
					this.$message({
						type: "error",
						message: "查询失败"
					});
				})
				.finally(() => (this.loading = false));
		},

		/**
		 * 列表导出
		 */
		exportReg(params = { size: 9999 }) {
			let req = { ...this.params, ...params };
			exportWarehousing(req).then(res => {
				// 关闭loading
				this.loading = false;
				// 此处有个坑。这里用content保存文件流，最初是content=res，但下载的test.xls里的内容如下图1，
				// 检查了下才发现，后端对文件流做了一层封装，所以将content指向res.data即可
				// 另外，流的转储属于浅拷贝，所以此处的content转储仅仅是便于理解，并没有实际作用=_=
				const content = res.data;
				const blob = new Blob([content]); // 构造一个blob对象来处理数据
				const fileName = "待变更资产信息记录.xls"; // 导出文件名
				// 对于<a>标签，只有 Firefox 和 Chrome（内核） 支持 download 属性
				// IE10以上支持blob但是依然不支持download
				if ("download" in document.createElement("a")) {
					// 支持a标签download的浏览器
					const link = document.createElement("a"); // 创建a标签
					link.download = fileName; // a标签添加属性
					link.style.display = "none";
					link.href = URL.createObjectURL(blob);
					document.body.appendChild(link);
					link.click(); // 执行下载
					URL.revokeObjectURL(link.href); // 释放url
					document.body.removeChild(link); // 释放标签
				} else {
					// 其他浏览器
					navigator.msSaveBlob(blob, fileName);
				}
				this.btnSendTem = false;
			});
		},
		//  * 码值转换
		dictVal_assetClassification(row, column, cellValue) {
			let obj = getDict("asset-classification").find(p => p.value == cellValue);
			if (obj) {
				return obj.label;
			}
			return "";
		},

		//  * 码值转换
		dictVal_useStatusList(row, column, cellValue) {
			let obj = getDict("asset-use-status").find(p => p.value == cellValue);
			if (obj) {
				return obj.label;
			}
			return "";
		}
	}
};
